<?php

class Model {

	var $db = null;
    var $port = null;
	var $user = null;
	var $pass = null;
    
    var $logger;
    
	public function __construct($host , $port , $dbname ,  $user , $pass){
        $this->logger = new KLogger ( "log/vocabolario.log" , KLogger::DEBUG );
		try {
			$this->db = new PDO("mysql:host=$host;port=$port;dbname=$dbname", $user, $pass );
			$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->db->exec("SET CHARACTER SET utf8");
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage();
			die();
		}
	}
	
    public function close(){
        $this->db=null;
    }
    
	public function insert($parola,$definizione,$uid){
		try {
			$sql ="INSERT INTO vocabolario (parola, definizione ,uid) VALUES (:parola,:definizione,:uid)";
			$stm = $this->db->prepare($sql);
			$stm->execute(array(":parola"=>$parola,":definizione"=>$definizione, ":uid"=>$uid));
		} catch (PDOException $e) {
			error_log($e);
			return false;
		}
		return true;
	}
    
    public function edit($id,$parola,$definizione,$uid){
        try {
            $sql ="UPDATE vocabolario SET parola=:parola, definizione=:definizione WHERE (id=:id AND uid=:uid)";
            $stm = $this->db->prepare($sql);
            $stm->execute(array(":id"=>$id,":parola"=>$parola,":definizione"=>$definizione, ":uid"=>$uid));
        } catch (PDOException $e) {
            error_log($e);
            return false;
        }
        return true;
    }
    
    public function delete($id,$uid) {
        // if(isset($SESSION['$id'])){
            // return true;
        // }
        try {
            $sql ="DELETE FROM vocabolario WHERE (id=:id AND uid=:uid)";
            $stm = $this->db->prepare($sql);
            $stm->execute(array(":id"=>$id,":uid"=>$uid));
        } catch (PDOException $e) {
            error_log($e);
            return false;
        }
        return false;
    }
    
    public function loginUser($SESSION) {
        if(isset($SESSION['LOGIN_USER'])){
            return true;
        }
        return false;
    }
    
    public function checkLogin($user,$password,$users) {
        $uid = false;
        foreach ($users as $u) {
            if($user==$u->name){
                if($password==$u->password){
                    $check=$u->uid;
                    break;
                }
            }
        }
        return $uid;
    }
    
    public function update($id,$parola,$definizione){
        // TODO
    }
    
    public function genTest($num,$uid){
	    $output = array();
    	try {

    	    $this->logger->LogDebug("num:$num");
            $this->logger->LogDebug("uid:$uid");
            
            $sql ="SELECT id,parola,definizione FROM vocabolario WHERE uid=:uid  ORDER BY RAND() LIMIT :len";
            $stm = $this->db->prepare($sql);
            $stm->bindValue(':len', $num, PDO::PARAM_INT);
            $stm->bindValue(':uid', $uid, PDO::PARAM_INT);
            $stm->execute();
            $result = $stm->fetchAll();
            $this->logger->LogDebug("result: ".count($result));
            for($i=0;$i<count($result);$i++){
				$question = (object) array('id'=>$result[$i][0], 'answer' => $result[$i][2],"response"=>$result[$i][1]); //,"choice"=>$choice);
            	array_push($output,$question);
            }
        } catch (PDOException $e) {
            error_log($e);
            $output = (object) array('question' => array());
        }
        return $output;
	}
    
    // AJAX
    public function checkTest($id,$val){
    	$count=0;   
        $this->logger->LogDebug("id:$id");
        $this->logger->LogDebug("val:$val");
    	try {
        	$sql ="SELECT count(*) FROM vocabolario WHERE id=:id  AND UPPER(parola)=UPPER(:parola)";
            $stm = $this->db->prepare($sql);
            $stm->bindValue(':parola', trim($val));
            $stm->bindValue(':id', $id, PDO::PARAM_INT);
            $stm->execute();
            $count = $stm->fetchColumn();
            $this->logger->LogDebug("count:$count");
        } catch (PDOException $e) {
            $this->logger->LogError($e);
            $output = (object) array('check' => false);
        }
        $res = ($count==1)?true:false;
        $this->logger->LogDebug("res:$res");
        return $res;
    }

}

?>